<template>
	<view class="main">
		<view class="product_detail">
			<rich-text :nodes="imageList"></rich-text>
		</view>
		<view class="operate">
			<view class="price">
				<view class="aliprice">
					￥<text>{{productDetail.item_price_zfb}}</text>
				</view>
				<view class="originPrice">
					￥{{productDetail.item_price_shichang}}
				</view>
			</view>
			<view class="buyNow" @click="buyNow" v-if="!noCode">
				立即购买
			</view>
			<button class="buyNow" open-type="getAuthorize" @getAuthorize="onalipaySignin" v-if="noCode"
			 onError="onAuthError" scope='userInfo'
			>立即购买</button>
		</view>
	</view>
</template>

<script>
	import parse from "@/pages/component/html-parser.js"
	export default {
		data() {
			return {
				demo: {
					top: 0,
					height: 0
				},
				productDetail:{},
				item_id:"",
				imageList:[],
				noCode:true,
			}
		},
		created () {
			const demo = uni.getMenuButtonBoundingClientRect()
			this.demo.top = demo.top
			this.demo.height = demo.height
		},
		onLoad(option) {
			var userId = uni.getStorageSync('userId');
			this.noCode = !userId;
			this.getCurrentProduct(option.id);
			this.item_id = option.id
		},
		methods: {
			//授权
			onalipaySignin(res) {
				var that = this;
				uni.login({
					provider: 'alipay',
					success: function(loginRes) {
						console.log(loginRes); // 获取用户信息 
						uni.getUserInfo({
							provider: 'alipay',
							withCredentials:true,  
							success: (res)=> {
								 let userInfo = JSON.parse(res.response).response;
								 uni.setStorageSync('userInfo', JSON.parse(res.response).response);
								  that.userInfo = userInfo;
								  that.noCode = false;
								 that.register(loginRes.authCode);
								 
							}
						});
					}
				});
			
			},
			register(authCode){
				this.$get('api_customer_register.ashx',{
					id:authCode,
					Pic:this.userInfo.avatar,
					Tel:this.userInfo.Tel,
					City:this.userInfo.city,
					Name:this.userInfo.nickName,
					Gpsx:this.nowLongitude || "",
					Gpsy:this.nowLatitude || "",
					// Tel:""
					},(res)=>{
						uni.setStorageSync('userId', res.data);
						this.buyNow()
				})
			},
			//获取当前套餐的产品详情
			getCurrentProduct(item_id){
				this.$get('api_items_detail.ashx',{item_id},(res)=>{
					this.productDetail = res.data[0];
					var nodesList = parse(this.productDetail.item_beizhu);
					console.log(nodesList)
					uni.setNavigationBarTitle({
					　　title:this.productDetail.item_name
					})
					this.imageList = nodesList;
				})
			},
			//点击立即购买
			buyNow(){
				var that = this;
				this.$get('order.ashx',{
					item_id:this.item_id,
					id:uni.getStorageSync('userId')
				},(res)=>{
					var data = res.data;
					var orderInfo = data.alipay_trade_create_response.trade_no;
					console.log(orderInfo);
					uni.requestPayment({
						provider: 'alipay', // 服务提供商 支付宝支付: alipay   微信支付: wxpay
						orderInfo: orderInfo, // 支付宝订单号
						success: function (res) {
							 console.log('success:' + JSON.stringify(res));
							 if(res.resultCode == 9000 ){
								 that.$get('api_items_succ.ashx',{item_id:that.item_id,id:uni.getStorageSync('userId'),order_id:orderInfo},(res)=>{
								 	var data = res.data;
								 	uni.showToast({
								 		title:'支付成功！',
								 		icon:'none'
								 	})
								 	uni.navigateTo({
								 		url:'/pages/buySuccess/buySuccess?id='+data.quan_id
								 	})
								 })
							 }else{
								 uni.showToast({
								 	title:'支付失败！',
								 	icon:'none'
								 })
							 }
						},
						fail: function (err) {
							console.log('fail:' + JSON.stringify(err))
						}
					})
				})
				
				
				
			}
		}
	}
	</script>

<style lang="scss">
    .demo{
        position: relative;
        display: flex;
        align-items: center;
        justify-content: center;
        background: #fff;
        font-size: 26rpx;
        .left{
            float: left;
            position: absolute;
            width: max-content;
            height: max-content;
            top: 0;
            bottom: 0;
            left: 20rpx;
            margin: auto;
            .iconfont{
                color: #3C3C3C;
            }
        }
    }
</style>
<style>
	.operate {
		position: fixed;
		bottom: 0;
		left: 0;
		width: 750rpx;
		height: 97rpx;
		line-height: 97rpx;
		background: #fff;
		border: 1rpx solid #eeeeee;
		padding: 0 24rpx;
		box-sizing: border-box;
		display: flex;
		justify-content: space-between;
		align-items: center;
	}
    .price view {
		display: inline-block;
	}
	.aliprice {
		font-size: 30rpx;
		color: #ff6010;
	}
	.aliprice text {
		font-size: 54rpx;
		font-weight: bold;
	}
	.originPrice {
		font-size: 30rpx;
		color: #999999;
		text-decoration: line-through;
		margin-left: 12rpx;
		font-weight: bold;
	}
	.buyNow {
		width: 180rpx;
		height: 70rpx;
		line-height: 70rpx;
		color: #fff;
		text-align: center;
		background: #fc9a30;
		font-size: 30rpx;
		border-radius: 10rpx;
	}
	.product_detail {
		margin-bottom: 100rpx;
	}
</style>
